package com.iflytek.pet.biz.controller;

import com.iflytek.pet.biz.entity.login.Account;
import com.iflytek.pet.biz.manager.AccountManagerService;
import com.iflytek.pet.common.resp.ApiResponse;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: JohnRambo
 * @description: 账户管理
 * @date: 2024/5/26 08:01
 * @version: 1.0
 */
@RestController
@RequestMapping("/api/account")
public class AccountController {

    @Resource
    private AccountManagerService accountManagerService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("list")
    public ApiResponse<Account> getInfo(){
        try {
            String key = "pet:user:admin";
            List<Account> accountList = accountManagerService.getAccountList();
            redisTemplate.opsForValue().set(key, accountList.toString(), 5, TimeUnit.MINUTES);

            Object user = redisTemplate.opsForValue().get(key);
            System.out.println("user =" + user);
            return ApiResponse.success(accountList);
        } catch (Exception e){
            return ApiResponse.error(e.getMessage());
        }
    }

    @PostMapping("add")
    public ApiResponse<Boolean> newAccount(@RequestBody Account req){
        try {
            Boolean flag = accountManagerService.addAccount(req);
            return ApiResponse.success(flag);
        } catch (Exception e){
            return ApiResponse.error(e.getMessage());
        }
    }


}